<script setup lang="ts">
import { onMounted } from 'vue';
import { getSysImgUrl } from '/@/utils/index';
import dayjs from 'dayjs';
import { navigateTo, showLoading } from '@tarojs/taro';
import { Tips as AppNutIconTips, Eye as AppNutIconEye } from '@nutui/icons-vue-taro';
import { MESSAGE_INFORMATION_QUERY } from '/@/api/modules/base/message-information';
import { state } from './state';

/**
 * @description 资讯查询
 * @param {Number} page -当前页
 * @param {Number} pageSize -每页条数
 */
function queryList(page: number = 1, pageSize: number = 15) {
  if (!page && state.finished) return;
  state.loading = true;
  showLoading();
  MESSAGE_INFORMATION_QUERY({
    comQuery: { page, pageSize },
    dto: { publishStatus: 2 },
  })
    .then((res) => {
      state.loading = false;

      if (
        state.activePage >=
        (res.comQuery?.pageCount ||
          (res.comQuery?.totalCount as number) / (res.comQuery?.pageSize as number) ||
          1)
      )
        state.finished = true;
      if (!res.dto || !res.dto.length) return;

      res.dto.forEach((item) => state.list.push(item));

      state.activePage += 1;
    })
    .catch(() => {
      state.loading = false;
    });
}

/**
 * @function 加载
 */
function onLoading() {
  if (state.finished) return;

  queryList(state.activePage);
}

onMounted(() => {
  state.activePage = 1;
  state.loading = true;
  state.finished = false;
  state.list = [];
  onLoading();
});
</script>

<template>
  <view class="information-list app-page--1">
    <app-navbar title="资讯列表" />
    <scroll-view
      class="app-page__wrapper"
      :scroll-y="true"
      :lower-threshold="100"
      @scrolltolower="onLoading()"
    >
      <view class="information-list__wrapper">
        <view
          class="information-item"
          v-for="item in state.list"
          @click="
            navigateTo({
              url: `/pages/information/detail/index?id=${item.id}`,
            })
          "
        >
          <nut-avatar shape="square" size="large">
            <image
              v-if="item.coverUrl"
              :src="getSysImgUrl(item.coverUrl)"
              class="information-item__image"
              mode="aspectFill"
            />
            <app-nut-icon-tips v-else />
          </nut-avatar>
          <view class="information-item__right">
            <view class="information-item__title"
              ><text>{{ item.title }}</text></view
            >
            <view class="information-item__footer">
              <text class="information-item__footer--left">
                {{ dayjs(Number(item.createTime)).format('YYYY-MM-DD') || '_' }}
              </text>
              <view class="information-item__footer--right">
                <app-nut-icon-eye />&nbsp;&nbsp;<text>{{ item.readCount }}</text>
              </view>
            </view>
          </view>
        </view>
      </view>

      <view class="app-page__list-tips">
        <text v-if="!state.list.length">暂无数据！</text>
        <text v-else-if="state.list.length && state.loading">加载中...</text>
        <text v-else-if="state.list.length && state.finished">加载完成!</text>
      </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
